package algorithms.leecode;

public class Au43 {
    public static void main(String[] args) {
        System.err.println(multiply("1","1"));
    }
    public static String multiply(String num1, String num2) {
        char[] num1Arr = num1.toCharArray();
        char[] num2Arr = num2.toCharArray();
        char[] res1 = new char[10];
//        for (int i = 0; i < res1.length; i++) {
//            res1[i] = '0';
//        }
        long res = 0;
        for(int i = 0; i< num1Arr.length;i++) {
            char x = num1Arr[i] ;
            long indx = (long)Math.pow(10,num1Arr.length - i - 1);
            for(int j = 0; j < num2Arr.length;j++){
                char y = num2Arr[j];
                long indy = (long)Math.pow(10,num2Arr.length - j - 1);
                int index = (num1Arr.length - i - 1) + (num2Arr.length - j - 1);
                accumate(index,res1,(x - '0') *(y- '0'));

            }
        }
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < res1.length; i++) {
            if (res1[i] >= '0') {
                builder.append(res1[i]);
            }
        }

        return builder.reverse().toString();
    }

    public static void accumate(int index,char[] res1,int current) {
        char res11 = res1[index];
        // 顶多两位数
        int result;
        if (res11 < '0') {
             result = res11 + current;
        } else {
            result = res11 - '0' + current;
        }
        res1[index] = '0';
        int index1 = index;
        int tmp = 0;
        while (result / 10 > 0) {
            int less = result % 10;
            res1[index1++] = (char)(less +  '0');
            result = result / 10;
            if (res1[index1] >= '0') {
                result = res1[index1] - '0' + result;
            } else {
                result = res1[index1] + result;
            }
            res1[index1] = '0';

        }
        res1[index1] = (char) (res1[index1] + result );
    }
}
